package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.model.BaseAttrInfo;
import com.atguigu.gmall.product.model.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-04-15 15:30
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService {

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    /**
     * 根据多个 三级分类ID 查询当前分类包含平台属性名列表(包含平台属性值列表)
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        //1.获取持久层对象调用持久层自定义查询方法
        BaseAttrInfoMapper baseAttrInfoMapper = this.getBaseMapper();
        //2.返回结果
        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoMapper.getAttrInfoList(category1Id, category2Id, category3Id);
        return baseAttrInfoList;
    }

    /**
     * 保存平台属性以及平台属性值列表
     * 1.todo 判断新增平台属性是否存在
     * 2.判断是否有平台属性ID提交-提交:修改操作 没提交:新增
     * 3.新增操作
     * 新增平台属性记录到平台属性表base_arttr_info表
     * 批量新增平台属性值记录到base_atttr_value表
     * 4.修改操作
     * 先删除所有平台属性值再新增
     *
     * @param baseAttrInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1.判断是否有平台属性ID提交-提交:修改操作 没提交:新增
        if (baseAttrInfo.getId() == null) {
            //2.新增操作
            //2.1 新增平台属性记录到平台属性表base_arttr_info表  主键回查机制 <SelectKey>   SELECT LAST_INSERT_ID()
            this.save(baseAttrInfo);

            //2.2 批量新增平台属性值记录到base_atttr_value表
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)) {
                attrValueList.stream().forEach(baseAttrValue -> {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                });
                //批量保存  insert into xxx values(?,?)(?,?)
                baseAttrValueService.saveBatch(attrValueList);
            }
        } else {
            //4.修改操作
            this.updateById(baseAttrInfo);
            //先删除所有平台属性值再新增
            LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BaseAttrValue::getAttrId, baseAttrInfo.getId());
            baseAttrValueService.remove(queryWrapper);

            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)) {
                attrValueList.stream().forEach(baseAttrValue -> {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                });
                //批量保存  insert into xxx values(?,?)(?,?)
                baseAttrValueService.saveBatch(attrValueList);
            }

        }
    }


    /**
     * 根据平台属性ID 查询平台属性值列表
     *
     * @param attrId
     * @return
     */
    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseAttrValue::getAttrId, attrId);
        return baseAttrValueService.list(queryWrapper);
    }
}
